var _j = {
    isYear: {
        expression: /^(19|20)\d{2}$/,
        text: '只能为年份(四位,1900-2099)',
        perform: function (str) {
            var re = new RegExp(_j.isYear.expression);
            return re.test(str) ? true : false;
        }
    },
    isNumber: {
        expression: /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/,
        text: '只能为数字',
        perform: function (str) {
            var re = new RegExp(_j.isNumber.expression);
            return re.test(str) ? true : false;
        }
    },
    isBankNum: {
        expression: /^\d{16}|\d{19}$/,
        text: '格式错误',
        perform: function (str) {
            var re = new RegExp(_j.isBankNum.expression);
            return re.test(str) ? true : false;
        }
    },
    isTelephone: {
        expression: /^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$/,
        text: '格式错误',
        perform: function (str) {
            var re = new RegExp(_j.isTelephone.expression);
            return re.test(str) ? true : false;
        }
    },
    isInteger: {
        expression: /^[0-9]*$/,
        text: '只能为正整数',
        perform: function (str) {
            var re = new RegExp(_j.isInteger.expression);
            return re.test(str) ? true : false;
        }
    },
    isIp: {
        expression: /^(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)$/,
        text: '格式错误',
        perform: function (str) {
            var re = new RegExp(_j.isIp.expression);
            return re.test(str) ? true : false;
        }
    },
    isPort: {
        expression: /^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-5]{2}[0-3][0-5])$/,
        text: '格式错误',
        perform: function (str) {
            var re = new RegExp(_j.isPort.expression);
            return re.test(str) ? true : false;
        }
    },
    notNull: {
        expression: "^[ ]+$",
        text: '不能为空',
        perform: function (str) {
            if (str == "" || str == undefined || str == null || str == NaN) return false;
            var re = new RegExp(_j.notNull.expression);
            return !re.test(str);
        }
    },
    isPhone: {
        expression: /^[1][0-9]{10}$/,
        text: '格式错误',
        perform: function (str) {
            var re = new RegExp(_j.isPhone.expression);
            return re.test(str) ? true : false;
        }
    },
    isAllChinese: {
        expression: /^([\u4E00-\u9FA5]+，?)+$/,
        text: '只能为中文',
        perform: function (str) {
            var re = new RegExp(_j.isAllChinese.expression);
            return re.test(str) ? true : false;
        }
    },
    isHaveChinese: {
        expression: "[\\u4E00-\\u9FFF]+",
        text: '中不能含有汉字',
        perform: function (str) {
            var re = new RegExp(_j.isHaveChinese.expression);
            return re.test(str) ? false : true;
        }
    },
    isIdCard: {
        expression: [/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/, /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$/],
        text: '输入错误',
        perform: function (str) {
            var re1 = new RegExp(_j.isIdCard.expression[0]);
            var re2 = new RegExp(_j.isIdCard.expression[1]);
            return re1.test(str) || re2.test(str) ? true : false;
        }
    },
    isSpecial: {
        expression: ["~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "{", "}", "[", "]", "(", ")", ":", ";", "'", "|", "\\", "<", ">", "?", "/", "<<", ">>", "||", "//", "administrators", "administrator", "管理员", "系统管理员", "admin", "select", "delete", "update", "insert", "create", "drop", "alter", "trancate"],
        text: '不能包含特殊字符',
        perform: function (str) {
            str = str.toLowerCase();
            for (var i = 0; i < _j.isSpecial.expression.length; i++) {
                if (str.indexOf(_j.isSpecial.expression[i]) >= 0) {
                    return false;
                }
            }
            return true;
        }
    },
    isUrl: {
        expression: /^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+/,
        text: '格式错误',
        perform: function (str) {
            var re = new RegExp(_j.isUrl.expression);
            return re.test(str) ? true : false;
        }
    },
    isEmail: {
        expression: /^[-_A-Za-z0-9]+@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/,
        text: '格式错误',
        perform: function (str) {
            var re = new RegExp(_j.isEmail.expression);
            return re.test(str) ? true : false;
        }
    },
    custom: function (value, str) {
        var name = value[0];
        if (name == 'length') {
            if (value[1].indexOf('les') != -1) {
                var nums = value[1].replace('les', '');
                nums = parseInt(nums);
                var length = str.toString().length;
                return {
                    result: length >= nums ? true : false,
                    text: '不能小于' + nums + '位'
                };
            } else if (value[1].indexOf('gre') != -1) {
                var nums = value[1].replace('gre', '');
                nums = parseInt(nums);
                var length = str.toString().length;
                return {
                    result: length <= nums ? true : false,
                    text: '不能大于' + nums + '位'
                }
            };
        };
    },
    type: function (obj, type) {
        var result = null;
        switch (type) {
            case "string":
                result = (typeof obj == 'string') && obj.constructor == String;
                break;
            case "function":
                result = (typeof obj == 'function') && obj.constructor == Function;
                break;
            case "array":
                result = (typeof obj == 'object') && obj.constructor == Array;
                break;
            case "number":
                result = (typeof obj == 'number') && obj.constructor == Number;
                break;
            case "date":
                result = (typeof obj == 'object') && obj.constructor == Date;
                break;
            case "object":
                result = (typeof obj == 'object') && obj.constructor == Object;
                break;
            default:
                result = (typeof obj == 'string') && obj.constructor == String;
                break;
        };
        return result;
    },
    inArray: function (str, _array) {
        if ($.inArray(str, _array) == -1) {
            return false
        } else {
            return true
        }
    },
    isEqual: function (a, b) {
        if (a === b) {
            return a !== 0 || 1 / a === 1 / b;
        }
        if (a == null || b == null) {
            return a === b;
        }
        var A = Object.prototype.toString.call(a),
            B = Object.prototype.toString.call(b);
        if (A !== B) {
            return false;
        }
        function isObjectValueEqual(a, b) {
            // Of course, we can do it use for in 
            // Create arrays of property names
            var aProps = Object.getOwnPropertyNames(a);
            var bProps = Object.getOwnPropertyNames(b);

            // If number of properties is different,
            // objects are not equivalent
            if (aProps.length != bProps.length) {
                return false;
            }

            for (var i = 0; i < aProps.length; i++) {
                var propName = aProps[i];

                // If values of same property are not equal,
                // objects are not equivalent
                if (a[propName] !== b[propName]) {
                    return false;
                }
            }

            // If we made it this far, objects
            // are considered equivalent
            return true;
        };
        switch (A) {
            case '[object RegExp]':
            case '[object String]':
                return '' + a === '' + b;
            case '[object Number]':
                if (+a !== +a) {
                    return +b !== +b;
                }
                return +a === 0 ? 1 / +a === 1 / b : +a === +b;
            case '[object Date]':
            case '[object Boolean]':
                return +a === +b;
        }
        if (A == '[object Object]') {
            if (!isObjectValueEqual(a, b)) {
                return false;
            };
            return true;
        }
        if (A == '[object Array]') {
            if (a.toString() == b.toString()) {
                return true;
            }
            return false;
        }
    },
};

module.exports = _j;